<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >

    <el-form-item label="批次" class="pici" prop="id">
        <el-select
          v-model="queryParams.id"
          placeholder="请选择录入批次"
          clearable
          size="mini"
          @change="bigTypeChange"
        >
          <el-option
            v-for="item in idList"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分类" prop="bigType">
        <el-select
          v-model="queryParams.bigType"
          placeholder="请选择分类"
          clearable
          size="mini"
          @change="bigTypeChange"
        >
          <el-option
            v-for="item in bigTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类别" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择类别"
          clearable
          size="mini"
        >
        <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="商品代码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入商品代码"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="商品条码" prop="barCode">
        <el-input
          v-model="queryParams.barCode"
          placeholder="请输入商品条码"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品代码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入商品代码"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="interval" label="批发价" prop="wholesalePrice">
        <el-col :span="10">
          <el-form-item prop="greaterWholesalePrice">
            <el-input
              v-model="queryParams.greaterWholesalePrice"
              clearable
              size="mini"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="1" class="ico"> ~ </el-col>
        <el-col :span="10">
          <el-form-item prop="lessWholesalePrice">
            <el-input
              v-model="queryParams.lessWholesalePrice"
              clearable
              size="mini"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item class="interval" label="库存数量">
        <el-col :span="10">
          <el-form-item prop="greaterCount">
            <el-input
              v-model="queryParams.greaterCount"
              clearable
              size="mini"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="1" class="ico"> ~ </el-col>
        <el-col :span="10">
          <el-form-item prop="lessCount">
            <el-input
              v-model="queryParams.lessCount"
              clearable
              size="mini"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="排序" prop="orderFiled">
        <el-select
          v-model="queryParams.orderFiled"
          placeholder="请选择排序字段"
          clearable
          size="mini"
        >
        <el-option
            v-for="item in orderFiledList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="form_button">
        <el-button ref='order' 
          type="primary"
          size="mini"
          @click="orderClick"
          >{{orderText}}</el-button>
        
      </el-form-item>


      <el-form-item class="form_button">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['exam:inventory:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload2"
          size="mini"
          :loading="exportLoading"
          @click="uploadOpen = true"
          v-hasPermi="['exam:inventory:export']"
          >导入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload2"
          size="mini"
          :loading="exportLoading"
          @click="InventoryContrast"
          v-hasPermi="['exam:inventory:export']"
          >测试</el-button
        >
      </el-col>
      <el-col  class="textSum" :span="1.5">
        <p><span>总库存数：</span>{{listCum.countSum}}</p>
      </el-col>
      <el-col class="textSum" :span="1.5">
        <p><span>批发总金额：</span>{{listCum.retailPriveSum}}</p>
      </el-col>
      <el-col class="textSum" :span="1.5">
        <p><span>零售价金额：</span>{{listCum.wholesalePriceSum}}</p>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="inventoryList"

    >
      <el-table-column label="分类" align="center" prop="bigType" />
      <el-table-column label="类别" align="center" prop="type" />
      <el-table-column label="商品代码" width="90" align="left" prop="code" />
      <el-table-column
        label="商品条码"
        width="130"
        align="left"
        prop="barCode"
      />
      <el-table-column
        width="150"
        label="商品名称和规格"
        align="left"
        prop="nameSpecification"
      >
        <template slot-scope="scope">
          <MyTooltip :text="scope.row.nameSpecification"></MyTooltip>
        </template>
      </el-table-column>
      <el-table-column label="库存数量" align="left" prop="count" />
      <el-table-column label="商品单位" align="left" prop="unit" />
      <el-table-column label="保质期" align="left" prop="qualityTerm" />
      <el-table-column
        label="入库时间"
        align="left"
        prop="warehousingTime"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.warehousingTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="入表时间"
        align="center"
        prop="crtTime"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.crtTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="(单品)利润" width="110" align="left" prop="profit" />
      <el-table-column label="(库存)利润" width="110"  align="left" prop="profitSum" />
      <el-table-column label="利润率" align="left" prop="profitMargin" />
      <el-table-column label="批发价" align="left" prop="wholesalePrice" />
      <el-table-column label="建议零售价" width="100" align="left" prop="msrpPrice"/>
      <el-table-column label="门店零售价" width="100" align="left" prop="retailPrive"/>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 导入的对话框 -->
    <el-dialog
      title="批量导入"
      :visible.sync="uploadOpen"
      width="500px"
      append-to-body
      :before-close="uploadClose"
    >
      <el-form :inline="true" label-width="68px">
        <el-upload
          ref="upload"
          class="upload-demo"
          :action="uploadFileUrl"
          :limit="1"
          :on-exceed="handleExceed"
          :on-remove="handleRemove"
          :on-success="uploadSuccess"
          :file-list="fileList"
          :headers="headers"
          :auto-upload="false"
        >
          <el-button slot="trigger" size="small" type="primary"
            >选取文件</el-button
          >
          <el-button
            style="margin-left: 10px"
            size="small"
            type="success"
            @click="submitUpload"
            >上传到服务器</el-button
          >
          <div slot="tip" class="el-upload__tip">
            只能上传xls,csv,xlsx文件，且不超过50M
          </div>
        </el-upload>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="uploadClose()">确 定</el-button>
        <el-button @click="uploadClose()">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInventory, getType,getids,listSum ,inventoryContrast} from "@/api/meiyijia/inventory";
import { getToken } from "@/utils/auth";

import "./index.scss";
export default {
  name: "Inventory",
  data() {
    return {
      // 显示统计数量
      listCum:{},
      // 排序字段
      orderText:"正序",
      // 排序字段列表
      orderFiledList:[
        {"label":"库存数量","value":"count"},
        {"label":"保质期","value":"quality_term"},
        {"label":"入库时间","value":"warehousing_time"},
        {"label":"入表时间","value":"crt_time"},
        {"label":"利润率","value":"profitMargin"},
        {"label":"库存利润","value":"profitSum"},
        {"label":"单品利润","value":"profit"},
        {"label":"批发价","value":"wholesale_price"}

      ],
      // 批次的下拉框
      idList:[],
      // 类别的下拉框
      typeList:[],
      //分类的下拉框
      bigTypeList: [],
      // 导入的遮罩层
      uploadOpen: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 美宜佳现有库存表格数据
      inventoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bigType: null,
        type: null,
        barCode: null,
        lessCount:null,
        greaterCount:null,
        lessWholesalePrice:null,
        greaterWholesalePrice:null,
        id:null,
        order:'asc'
      },

      // 导入上传文件需要使用到的参数
      uploadFileUrl:
        process.env.VUE_APP_BASE_API + "/meiyijia/inventory/upload",
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      //文件列表
      fileList: [],
    };
  },
  created() {
    this.getType();
    this.getList();
  
  },
  methods: {
      // 获取库存差异表
      InventoryContrast(){
      var param ={
        preId:"20230521892272688",
        aftId:"20230520100164929",
        type:1
      }
      inventoryContrast(param).then((response) => {
        console.log(response)
      });
    },
    orderClick(){
      if(this.queryParams.order == 'desc'){
        this.queryParams.order = 'asc'
        this.orderText = "正序"
      }else if(this.queryParams.order == 'asc'){
        this.queryParams.order = 'desc'
        this.orderText = "倒序"
      }
      this.getList();
    },
    bigTypeChange(value){
     var list =  this.bigTypeList.filter(item=>item.value == value)
     if(list.length>0){
     this.typeList = list[0].child
     }else {
      this.typeList = []
      this.queryParams.type = ""
     }
    },

    // 获取分类和类别的 下拉框的值
    getType() {
      getType().then((response) => {
        this.bigTypeList = response.data
      });
      getids().then((response) => {
        this.idList = response.data
        this.queryParams.id = response.data[0]
      })
    },
    

    // 上传的的弹窗关闭的时候 清空导入的文件的选择的列表
    uploadClose() {
      this.uploadOpen = false;
      this.fileList = [];
    },
    // 手动上传的提交
    submitUpload() {
      this.$refs.upload.submit();
    },
    //上传的文件移除事件
    handleRemove(file, fileList) {
      deleteFile({
        name: file.response.data.fileName.replace("profile", ""),
      }).then((res) => {
        console.log("object :>> ", res);
      });
    },
    //上传成功后的回调事件
    uploadSuccess(data) {
      console.log(data);
      if (data.code === 200) {
        this.$message.warning("导入成功");
        this.uploadClose();
      }
    },
    //上传的前置事件
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },

    /** 查询美宜佳现有库存列表 */
    getList() {
      this.loading = true;
      listInventory(this.queryParams).then((response) => {
        this.inventoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });


      listSum(this.queryParams).then(response=>{
        this.listCum = response.data;
        this.loading = false;
      })

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有美宜佳现有库存数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportInventory(queryParams);
        })
        .then((response) => {
          this.$download.name(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped lang="scss">
</style>